package com.osdp.gateway.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.osdp.gateway.service.GatewayRouteService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.route.RouteDefinition;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 用@RequestParam请求接口时,主要用于接收http://host:port/path?参数名=参数值数据,URL是:http://www.test.com/user/getUserById?userId=1
 *
 * 用@PathVariable请求接口时,主要用于接收http://host:port/path/{参数值}数据,URL是:http://www.test.com/user/getUserById/2
 * 主要应用场景：不少应用为了实现RestFul的风格
 */
@RestController
@RequestMapping("/gateway")
public class GatewayController {

    @Autowired
    private GatewayRouteService gatewayRouteService;

    @GetMapping("/index")
    public String hello(){
        System.out.println("------------------hello---------");
        return "hellow gateway!";
    }

    @RequestMapping("/route/get/{tenantId}")
    public String getTenantRoutes(@PathVariable("tenantId") String tenantId){
        if(StringUtils.isEmpty(tenantId)) return "";
        List<RouteDefinition> tenanlList = gatewayRouteService.getTenantRouteDefinitions(Arrays.asList(tenantId));
        return JSONArray.toJSONString(tenanlList);
    }
    @RequestMapping("/route/load/{tenantId}/{routeId}")
    public Object loadTenantRoute(@PathVariable("tenantId") String tenantId,
                                  @PathVariable("routeId")String routeId){
        if(StringUtils.isEmpty(tenantId) || StringUtils.isEmpty(routeId)){
            return null;
        }
        return JSONArray.toJSONString(gatewayRouteService.getTenantRouteDefinition(tenantId,routeId));
    }
    @RequestMapping("/tenant/roudeIds")
    public List<String> getTenantRouteIds(@RequestBody List<String> tenantIds){
        return gatewayRouteService.getRouteIds(tenantIds);
    }
    @RequestMapping("/route/remove/{tenantId}/{routeId}")
    public Boolean removeTenantRoute(@PathVariable("tenantId") String tenantId, @PathVariable("routeId") String routeId){
        if(StringUtils.isEmpty(tenantId) || StringUtils.isEmpty(routeId)){
            return false;
        }
        try {
            gatewayRouteService.removeTenantRouteDefinition(tenantId, routeId);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @RequestMapping("/route/put/{tenantId}")
    public Boolean putTenantRoute(@PathVariable("tenantId") String tenantId, @RequestBody JSONObject object) {
        RouteDefinition route = object.toJavaObject(RouteDefinition.class);
        if(StringUtils.isEmpty(tenantId) || StringUtils.isEmpty(route.getId())){
            return false;
        }
        try{
            gatewayRouteService.addTenantRouteDefinition(tenantId,route);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    @RequestMapping("/route/update/{tenantId}")
    public Boolean updateTenantRoute(@PathVariable("tenantId") String tenantId, @RequestBody JSONObject object){
        RouteDefinition route = object.toJavaObject(RouteDefinition.class);
        if(StringUtils.isEmpty(tenantId) || StringUtils.isEmpty(route.getId())){
            return false;
        }
        try{
            gatewayRouteService.updateTenantRouteDefinition(tenantId,route);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
